<?php

declare(strict_types=1);

namespace app\common\service\sys;

use app\common\traits\ServiceTrait;
use app\common\dao\sys\FolderTypeDao;
use app\common\util\TreeUtil;

/**
 * 我的文件夹 服务业务层(Service)
 * @author kaige
 * @update  2023-09-01
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class FolderTypeService
{
  protected $dao = null;

  public function __construct()
  {
    $this->dao = new FolderTypeDao();
  }
  use ServiceTrait;

  /**
   * 重写 搜索条件
   * @param $post
   * @return mixed
   */
  public function _list_where($post)
  {
    $where = [];
    $whereOr = [];
    $order = ['id' => 'asc'];
    $field = ['*,pid as parentId'];

    if (isset($post['user_id']) && !empty($post['user_id'])) {
      $where[] = ['user_id', '=', $post['user_id']];
    } else {
      $where[] = ['user_id', '=', 0]; //否则一个不显
    }
    $whereOr[] = ['id', '=', 1];
    $result['whereOr'] = $whereOr;
    $result['where'] = $where;
    $result['field'] = $field;
    $result['order'] = $order;
    return $result;
  }

  /**
   * 获取树型
   */
  public function typeTree()
  {
    $tree = [];
    $list = $this->dao->getListAll(true, false, ['name,id as value,id,pid']);
    if ($list) {
      foreach ($list as $k => $v) {
        $list[$k]['isShow'] = true;
      }
      $tree = TreeUtil::list_to_tree($list, 0, 'id', 'pid', 'children');
    }
    return $tree;
  }

  public function getNamebyId($id = 0)
  {
    if ($id === 0) return '';
    return $this->dao->getNamebyId($id);
  }
}
